package Jw20241002;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description: 约瑟夫环
 * User: 王圆豪
 * Date: 2024-10-02
 * Time: 18:08
 */
public class Main {
    static class Node{
        int key;
        int num;
        Node next;

        public Node() {
        }

        public Node(int key) {
            this.key = key;
        }

        public Node(int key, Node next) {
            this.key = key;
            this.next = next;
        }
    }

    //根据数组创建环形链表
    public static Node create(int[] arr){
        Node head = new Node(arr[0]);
        head.num = 1;
        Node cur = head;
        for (int i = 1; i < arr.length; i++) {
            cur.next = new Node(arr[i]);
            cur.next.num = i + 1;
            cur = cur.next;
        }
        cur.next = head;
        return head;
    }

    //获取尾节点
    public static Node getLast(Node head){
        Node cur = head;
        while(cur.next != head){
            cur = cur.next;
        }
        return cur;
    }

    //开始报数出列
    public static int[] remove(Node head, int n){
        Node cur = head;
        Node prev = getLast(head);
        int count = 1;
        int m = 6;
        int[] arr = new int[n];
        int sz = 0;
        while(cur.next != cur){
            if (count == m){
                prev.next = cur.next;
                count = 1;
                m = cur.key;
                arr[sz++] = cur.num;
            }else {
                prev = cur;
                count++;
            }
            cur = cur.next;
        }
        arr[sz] = cur.num;
        return arr;
    }
    public static void main1(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        while (T != 0){
            int n = sc.nextInt();
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = sc.nextInt();
            }
            Node head = create(arr);
            int[] s = remove(head, n);
            System.out.print(s[0]);
            for (int i = 1; i < s.length; i++) {
                System.out.print(" " + s[i]);
            }
            System.out.println();
            T--;
        }
    }
}
